package com.sy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.sy.constant.MessageConstant;
import com.sy.constant.StatusConstant;
import com.sy.context.BaseContext;
import com.sy.dto.EmployeeDTO;
import com.sy.dto.EmployeeLoginDTO;
import com.sy.dto.EmployeePageQueryDTO;
import com.sy.mapper.EmployeeMapper;
import com.sy.pojo.Employee;
import com.sy.result.PageResult;
import com.sy.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
@Slf4j
public class EmployeeServiceImpl
        extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //1.根据用户名查询用户
        queryWrapper.eq(Employee::getUsername,employeeLoginDTO.getUsername());
        Employee employee = this.getOne(queryWrapper);
        if(employee==null){
            throw new RuntimeException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //2.前台获取到的密码进行加密
        String password =
                DigestUtils.md5DigestAsHex(employeeLoginDTO.getPassword().getBytes());
        //密码比对
        if(!password.equals(employee.getPassword())){
            throw new RuntimeException(MessageConstant.PASSWORD_ERROR);
        }
        //3.判断账号是否被锁定
        if(employee.getStatus() == StatusConstant.DISABLE){
            throw new RuntimeException(MessageConstant.ACCOUNT_LOCKED);
        }
        //4.判断当前账户是否被注销
        if(employee.getIsDelete()==StatusConstant.DISABLE) {
            throw new RuntimeException(MessageConstant.ACCOUNT_LOG_OFF);
        }
        return employee;
    }

    @Override
    public PageResult findAll(EmployeePageQueryDTO employeePageQueryDTO) {
        //根据用户名称做模糊查询
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        //判断员工的姓名是否为空，如果不为空就不做模糊查询，不为空做模糊查询
        queryWrapper.like(!StringUtils.isEmpty(employeePageQueryDTO.getName()),Employee::getName,employeePageQueryDTO.getName());
        //分页查询
        Page<Employee> page = this.page(new Page<>(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize()), queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void saveEmp(EmployeeDTO employeeDTO) {
        //将员工输入的员工信息赋值给员工对象
        Employee employee=new Employee();
        //将员工输入的员工信息EmployeeDTO，employee
        BeanUtils.copyProperties(employeeDTO,employee);
        //填充无数据的字段信息
        //设置初始密码 并对密码加密(MD5)
        String password=DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        //设置创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置创建人的id和修改人的id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        //提交到Mapper层实现保存操作  调用mapper层方法保存
        this.save(employee);

    }
}
